﻿namespace ChangchunPulbicServicesImp;

public class UserAccountServices : IUserAccountServices
{
    public UserAccountServices(IUserServices userServices)
    {
        UserServices = userServices;
    }

    public IUserServices UserServices { get; set; }

    public async Task<AlterPasswordResult> AlterPasswordAsync(User user, string newPassword)
    {
        ///参数验证
        if (!ValidationHelper.IsPasswordLegal(newPassword))
            return new AlterPasswordResult()
            {
                Message = "密码格式不符合要求",
                StatusCode = Code.IllegalArgs
            };

        ///登录验证
        var loginResult = await LoginAsync(user);
        if (!loginResult.Succeed)
        {
            return new AlterPasswordResult()
            {
                StatusCode = Code.WrongValidation,
                Message = "权限验证失败",
                LoginResult = loginResult,
            };
        }

        ///执行操作
        if (!(await UserServices.UpdateUserAsync(new User(user) {Password = newPassword })).Succeed)
            return new AlterPasswordResult()
            {
                StatusCode = Code.InternalFault,
                Message = "系统内部错误，更新密码失败",
                LoginResult = loginResult,
            };

        ///执行成功
        return new AlterPasswordResult()
        {
            StatusCode = Code.Succeed,
            Message = "更新密码成功",
            LoginResult = loginResult,
        };
    }

    public async Task<LoginResult> LoginAsync(User user)
    {
        ///从数据库获取用户信息
        var getUserResult = await UserServices.GetUserAsync(user.Account);
        if (!getUserResult.Succeed)
            return new LoginResult()
            {
                StatusCode = Code.InternalFault,
                Message = "内部错误，获取用户登录信息失败"
            };

        ///对比用户信息
        if (!user.Equals(getUserResult.ResultObject))
            return new LoginResult()
            {
                StatusCode = Code.WrongValidation,
                Message = "登录失败。登录信息错误",
            };

        ///登录成功
        return new LoginResult()
        {
            StatusCode = Code.Succeed,
            Message = "登陆成功",
            UserInfo = getUserResult.ResultObject,
        };
    }

    public async Task<RegisterResult> RegisterAsync(User user)
    {
        ///参数验证
        if (!ValidationHelper.IsUsernameLegal(user.Username))
            return new RegisterResult()
            {
                StatusCode = Code.IllegalArgs,
                Message = "用户名格式不符合要求"
            };
        if (!ValidationHelper.IsPasswordLegal(user.Password))
            return new RegisterResult()
            {
                StatusCode = Code.IllegalArgs,
                Message = "密码格式不符合要求"
            };

        ///向数据库增加实体
        var addUserResult = await UserServices.AddUserAsync(user);
        if (!addUserResult.Succeed)
            return new RegisterResult()
            {
                StatusCode = Code.InternalFault,
                Message = "内部错误，添加用户信息失败"
            };

        ///注册成功
        return new RegisterResult()
        {
            StatusCode = Code.Succeed,
            Message = "注册成功",
            NewUser = new User(user) { Account = addUserResult.NewKey },
        };
    }
}
